#!/bin/sh


TCPDUMP_PATH="/root/wireshark/"


DeleteFile()
{
	local count="`ls -l ${TCPDUMP_PATH} | wc -l`"
	while [ $count -gt 1000 ]; do
		rm -rf "${TCPDUMP_PATH}`ls -l ${TCPDUMP_PATH} | grep cap | busybox head -n 1 | awk '{ print $NF }'`"
		count="`ls -l ${TCPDUMP_PATH} | wc -l`"
	done
}


Main()
{
	if [ "$#" -ne 1 ]; then
		echo "$0 <client_ip>"
		exit 1
	fi
	
	mkdir -p ${TCPDUMP_PATH}
	
	local client_ip="$1"
	local begin_time=""
	local end_time=""
	local packname=""
	
	while [ 1 ]; do
		begin_time="`date +'%Y%m%d%H%M%S'`"
		end_time=""
		
		tcpdump -i any host $client_ip and -p icmp -nvv -s 0 -c 10 -w "${TCPDUMP_PATH}${begin_time}"
		
		end_time="`date +'%Y%m%d%H%M%S'`"
		packname="${TCPDUMP_PATH}$begin_time-$end_time.cap"
		
		mv "${TCPDUMP_PATH}${begin_time}" $packname
		
		DeleteFile
	done
}


Main $@
